/*
	BASS multi-speaker example
	Copyright (c) 2003-2014 Un4seen Developments Ltd.
*/

#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <io.h>
#include "bass.h"

HWND win=NULL;
HSTREAM chan = 0;

char filePath[MAX_PATH] = { '\0' };
char fileL[MAX_PATH] = { '\0' };
char fileR[MAX_PATH] = { '\0' };

float defLeftVol = 0;
float defRightVol = 0;

//DWORD flags[4]={BASS_SPEAKER_FRONT,BASS_SPEAKER_REAR,BASS_SPEAKER_CENLFE,BASS_SPEAKER_REAR2};

// display error messages
void Error(const char *es)
{
	char msg[200];
	sprintf(msg,"%s\n(error code: %d)", es, BASS_ErrorGetCode());
	MessageBox(win, msg, 0, 0);
}

int resetDefaultDevice()
{
	BASS_DEVICEINFO info;
	int index = 0, count = 0;

	for (index = 1; BASS_GetDeviceInfo(index, &info); index++) {
		if (info.flags&BASS_DEVICE_DEFAULT) { // device is enabled
#if DEBUG
			char msg[200];
			sprintf(msg, "Device: %s %d)", info.name, count);
			MessageBox(win, msg, 0, 0);
#endif
			break; // count it
		}
	}
	
	// before init, free it.
	BASS_Free();

	// initialize BASS - default device
	if (!BASS_Init(index, 44100, BASS_DEVICE_SPEAKERS, win, NULL)) {
		Error("Can't initialize audio device");
	}

	return 0;
}

BOOL CALLBACK dialogproc(HWND h, UINT m, WPARAM w, LPARAM l)
{
	switch (m) {
		case WM_COMMAND:
			switch (LOWORD(w)) {
				case 10: // left speaker
					resetDefaultDevice();
					if (access(fileL, 0)==0) {
						BASS_ChannelStop(chan);
						BASS_StreamFree(chan); // free old stream before opening new
						if (!(chan=BASS_StreamCreateFile(FALSE,fileL,0,0,BASS_SAMPLE_LOOP))) {
							Error("Can't play the file(1.mp3)");
							return 1;
						}
						//setVolumeLR(1.0, 0.0);
						BASS_ChannelSetAttribute(chan, BASS_ATTRIB_PAN, -1);
						BASS_ChannelPlay(chan, FALSE);
					}
					else {
						Error("1.mp3 is missing!");
					}
					return 1;

				case 11: //right speaker
				{
					resetDefaultDevice();
					if (access(fileR, 0)==0) {
						BASS_ChannelStop(chan);
						BASS_StreamFree(chan); // free old stream before opening new
						if (!(chan = BASS_StreamCreateFile(FALSE, fileR, 0, 0, BASS_SAMPLE_LOOP))) {
							Error("Can't play the file(2.mp3)");
							return 1;
						}
						//setVolumeLR(0.0, 1.0);
						BASS_ChannelSetAttribute(chan, BASS_ATTRIB_PAN, 1);
						BASS_ChannelPlay(chan, FALSE);
					}
					else {
						Error("2.mp3 is missing!");
					}
				}
				return 1;

				case IDCANCEL:
				case 12:
					if (chan != 0) {
						BASS_ChannelStop(chan);
						BASS_StreamFree(chan);
						chan = 0;
					}
					DestroyWindow(h);
					return 1;
			}
			break;

		case WM_INITDIALOG:
			BASS_INFO i;
			win = h;
			BASS_GetInfo(&i);
			if (i.speakers<4 && LOBYTE(GetVersion())<6) { // no extra speakers detected, enable them anyway? (on older Windows than Vista)
				if (MessageBox(0,"Do you wish to enable \"speaker assignment\" anyway?","No extra speakers detected",MB_ICONQUESTION|MB_YESNO)==IDYES) {
					// reinitialize BASS - forcing speaker assignment
					BASS_Free();
					if (!BASS_Init(-1, 44100, BASS_DEVICE_SPEAKERS, win, NULL)) {
						Error("Can't initialize device");
						DestroyWindow(win);
						break;
					}
				}
			}
			return 1;

		case WM_DESTROY:
			if(chan!=0){
				BASS_ChannelStop(chan);
				//setVolumeLR(defLeftVol, defRightVol);
				BASS_StreamFree(chan);
				chan = 0;
			}
			BASS_Free();
			break;
	}
	return 0;
}

int getAppDir()
{
	GetModuleFileName(NULL, filePath, MAX_PATH);
	char *p = strrchr(filePath, '\\');
	*p = '\0';
	strcpy(fileL, filePath);
	strcpy(fileR, filePath);
	strcat(fileL, "\\1.mp3");
	strcat(fileR, "\\2.mp3");
	//MessageBox(NULL, fileL, "Tip", 48);
	return 1;
}

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
	// check the correct BASS was loaded
	if (HIWORD(BASS_GetVersion())!=BASSVERSION) {
		MessageBox(0,"An incorrect version of BASS.DLL was loaded",0,MB_ICONERROR);
		return 0;
	}

	getAppDir();

	// main dialog
	DialogBox(hInstance,(char*)1000,0,&dialogproc);

	return 0;
}
